import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.asr.v20190614 import asr_client, models

secretId = "AKIDlMincGdjY6NN6ZmiNmulLegS4PTHo43l"
secretKey = "SdYCZfU2JK4WidGdjY6NM5YkeHdjZHju"

def trans_one_file(url):
    try:
        cred = credential.Credential(secretId, secretKey)
        httpProfile = HttpProfile()
        httpProfile.endpoint = "asr.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = asr_client.AsrClient(cred, "", clientProfile)

        req = models.CreateRecTaskRequest()
        params = {
            "EngineModelType": "16k_zh",
            "ChannelNum": 1,
            "ResTextFormat": 0,
            "SourceType": 0,
            "Url": url
        }
        req.from_json_string(json.dumps(params))

        resp = client.CreateRecTask(req)
        json_ = resp.to_json_string(indent=2)
        resp_dic = json.loads(json_)
        print(resp_dic)

        return dict(resp_dic)["Data"]["TaskId"]

    except TencentCloudSDKException as err:
        print(err)

def query_result(task_id):

    try:
        cred = credential.Credential(secretId, secretKey)
        httpProfile = HttpProfile()
        httpProfile.endpoint = "asr.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = asr_client.AsrClient(cred, "", clientProfile)

        req = models.DescribeTaskStatusRequest()
        params = {
        "TaskId":task_id
        }
        req.from_json_string(json.dumps(params))
        resp = client.DescribeTaskStatus(req)
        #print(resp)
        json_ = resp.to_json_string(indent=2)
        resp_dic = json.loads(json_)
        #print(resp_dic)
        return resp_dic["Data"]["Result"]

    except TencentCloudSDKException as err:
        print(err)


if __name__ == '__main__':
    url="https://audio-qianyang.bj.bcebos.com/000A7F1C-9712-4DF8-8397-877EF4436DBC_0.wav"
    task_id=trans_one_file(url)
    print(task_id)

    import time
    time.sleep(5)

    result=query_result(1488698872)
    print(result)